package com.crane.oss.school.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.crane.oss.api.bean.resp.TeacherResponse;
import com.crane.oss.school.syn.SynXunfeiService;
import com.crane.oss.sports.XunFeiPushEventApiService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crane.common.core.exception.AppException;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.exception.SysException;
import com.crane.common.core.utils.BCryptUtil;
import com.crane.common.core.utils.StringUtil;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.app.bean.request.appuser.AddAppUserReq;
import com.crane.oss.app.bean.request.appuser.UpdateAppUserReq;
import com.crane.oss.app.bean.resp.AppUserResp;
import com.crane.oss.app.service.AppUserService;
import com.crane.oss.enums.CommonYesOrNoEnum;
import com.crane.oss.enums.SexEnum;
import com.crane.oss.enums.TeacherStatusEnum;
import com.crane.oss.school.bean.request.QueryTeacherReq;
import com.crane.oss.school.bean.request.teacher.AddTeacherReq;
import com.crane.oss.school.bean.request.teacher.BatchOperateTeacherReq;
import com.crane.oss.school.bean.request.teacher.BatchImportTeacherEntity;
import com.crane.oss.school.bean.request.teacher.UpdateTeacherReq;
import com.crane.oss.school.bean.resp.AreaResp;
import com.crane.oss.school.bean.resp.ClassesInfoResp;
import com.crane.oss.school.bean.resp.OpenTeacherResp;
import com.crane.oss.school.bean.resp.OpenTeacherResp.OpenTeacher;
import com.crane.oss.school.bean.resp.TeacherExportResp;
import com.crane.oss.school.bean.resp.TeacherResp;
import com.crane.oss.school.bean.resp.TeacherWebDto;
import com.crane.oss.school.dao.ClassesInfoDao;
import com.crane.oss.school.dao.TeacherClassDao;
import com.crane.oss.school.dao.TeacherDao;
import com.crane.oss.school.model.ClassesInfo;
import com.crane.oss.school.model.Teacher;
import com.crane.oss.school.model.TeacherClass;
import com.crane.oss.school.service.AreaService;
import com.crane.oss.school.service.TeacherService;
import com.crane.utils.PinyinTool;

import lombok.extern.slf4j.Slf4j;

/**
 * @author zhenxing.pi
 * @email ccsu123456@qq.com
 * @date 2022年03月26日 14时24分54秒
 */
@Service
@Slf4j
public class TeacherServiceImpl implements TeacherService {

	@Resource
	protected TeacherDao teacherDao;

	@Resource
	protected ClassesInfoDao classesInfoDao;

	@Autowired
	@Lazy
	protected AppUserService appUserService;

	@Resource
	protected TeacherClassDao teacherClassDao;

	@Resource
	protected AreaService areaService;

	@Resource
	SynXunfeiService synXunfeiService;

	@Override
	public TeacherResp queryTeacher(Long teacherId) {
		log.info("TeacherServiceImpl queryTeacher teacherId={}", teacherId);
		Teacher teacher = teacherDao.selectById(teacherId);
		if (null == teacher) {
			throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "老师");
		}
		TeacherResp teacherResp = new TeacherResp();
		BeanUtil.copyProperties(teacher, teacherResp);

		AppUserResp appUserResp = appUserService.queryAppUser(teacher.getAppUserId());
		teacherResp.setPhone(appUserResp.getMobile());

		List<ClassesInfo> classesList = classesInfoDao.queryTeacherClasses(teacherId);
		teacherResp.setTeachClassesList(BeanUtil.copyList(classesList, ClassesInfoResp.class));

		return teacherResp;
	}

	@Override
	public TeacherResp queryTeacherByAppUserId(Long appUserId) {
		log.info("TeacherServiceImpl queryTeacherByAppUserId appUserId={}", appUserId);
		LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Teacher::getAppUserId,appUserId);
		Teacher teacher = teacherDao.selectOne(queryWrapper);
		if (null == teacher) {
			throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "老师");
		}
		TeacherResp teacherResp=new TeacherResp();
		BeanUtil.copyProperties(teacher,teacherResp);
		return teacherResp;
	}

	@Override
	public List<OpenTeacherResp> queryOpenTeacherList(QueryTeacherReq queryRequest) {
		List<OpenTeacherResp> returnList = new ArrayList<>();

		List<AreaResp> areaList = areaService.queryAreaList(queryRequest.getPartyId());
		for (AreaResp area : areaList) {
			LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(Teacher::getSchoolId, area.getSchoolId());
			queryWrapper.eq(ObjectUtils.isNotNull(queryRequest.getPartyId()), Teacher::getPartyId,
					queryRequest.getPartyId());
			if (StringUtil.isNotBlank(queryRequest.getName())) {
				queryWrapper.like(Teacher::getName, queryRequest.getName());
			}
			queryWrapper.orderByDesc(Teacher::getCreateTime);

			OpenTeacherResp resp = new OpenTeacherResp();
			resp.setSchoolId(area.getSchoolId());
			resp.setSchoolName(area.getAreaName());

			IPage<Teacher> page = teacherDao
					.selectPage(new Page<>(queryRequest.getPageNo(), queryRequest.getPageSize()), queryWrapper);

			IPage<OpenTeacher> result = new Page<>();
			result.setCurrent(queryRequest.getPageNo());
			result.setPages(queryRequest.getPageSize());

			resp.setTeacherList(page.convert(u -> {
				OpenTeacher v = new OpenTeacher();
				BeanUtils.copyProperties(u, v);
				return v;
			}));

			returnList.add(resp);
			queryWrapper.clear();
		}

		return returnList;
	}

	@Override
	public IPage<TeacherResp> queryTeacherList(QueryTeacherReq queryRequest) {
		log.info("TeacherServiceImpl queryList queryRequest={}", queryRequest);
		Map<String, Object> map = BeanUtil.asMap(queryRequest);
		map.put("offset", queryRequest.getPageSize() * (queryRequest.getPageNo() - 1));
		map.put("limit", queryRequest.getPageSize());
		int total = teacherDao.queryTeacherTotal(map);

		IPage<TeacherResp> result = new Page<>();
		result.setCurrent(queryRequest.getPageNo());
		result.setPages(queryRequest.getPageSize());
		List<TeacherResp> resultList = new ArrayList<>();
		result.setRecords(resultList);
		result.setTotal(total);
		if (total == 0) {
			return result;
		}

		List<Teacher> queryList = teacherDao.queryTeacherList(map);
		for (Teacher queryResp : queryList) {
			TeacherResp resultResp = new TeacherResp();
			BeanUtil.copyProperties(queryResp, resultResp);

			resultList.add(resultResp);
		}

		result.setRecords(resultList);
		return result;
	}

	@Override
	public IPage<TeacherExportResp> queryExportList(QueryTeacherReq queryRequest) {
		Map<String, Object> map = BeanUtil.asMap(queryRequest);
		map.put("offset", queryRequest.getPageSize() * (queryRequest.getPageNo() - 1));
		map.put("limit", queryRequest.getPageSize());
		int total = teacherDao.queryListTotal(map);

		IPage<TeacherExportResp> result = new Page<>();
		result.setCurrent(queryRequest.getPageNo());
		result.setPages(queryRequest.getPageSize());
		List<TeacherExportResp> resultList = new ArrayList<>();
		result.setRecords(resultList);
		result.setTotal(total);
		if (total == 0) {
			return result;
		}
		List<TeacherWebDto> queryList = teacherDao.queryList(map);

		for (TeacherWebDto queryResp : queryList) {
			TeacherExportResp resultResp = new TeacherExportResp();
			BeanUtil.copyProperties(queryResp, resultResp);

			resultResp.setSexName(SexEnum.getRemark(queryResp.getSex()));
			resultResp.setTypeName(CommonYesOrNoEnum.getRemark(queryResp.getType()));
			resultResp.setStatusName(TeacherStatusEnum.getRemark(queryResp.getStatus()));

			String classesStr = "";
			List<ClassesInfo> classesList = classesInfoDao.queryTeacherClasses(queryResp.getTeacherId());
			for (ClassesInfo info : classesList) {
				if (StringUtil.isNotEmpty(classesStr)) {
					classesStr = classesStr + "," + info.getClassName();
				} else {
					classesStr = info.getClassName();
				}
			}
			resultResp.setClassesStr(classesStr);
			resultList.add(resultResp);
		}

		result.setRecords(resultList);
		return result;
	}

	@Override
	public IPage<TeacherResp> queryList(QueryTeacherReq queryRequest) {
		log.info("TeacherServiceImpl queryList queryRequest={}", queryRequest);
		Map<String, Object> map = BeanUtil.asMap(queryRequest);
		map.put("offset", queryRequest.getPageSize() * (queryRequest.getPageNo() - 1));
		map.put("limit", queryRequest.getPageSize());
		int total = teacherDao.queryListTotal(map);

		IPage<TeacherResp> result = new Page<>();
		result.setCurrent(queryRequest.getPageNo());
		result.setPages(queryRequest.getPageSize());
		List<TeacherResp> resultList = new ArrayList<>();
		result.setRecords(resultList);
		result.setTotal(total);
		if (total == 0) {
			return result;
		}

		List<TeacherWebDto> queryList = teacherDao.queryList(map);

		for (TeacherWebDto queryResp : queryList) {
			TeacherResp resultResp = new TeacherResp();
			BeanUtil.copyProperties(queryResp, resultResp);

			resultResp.setSexName(SexEnum.getRemark(queryResp.getSex()));
			resultResp.setTypeName(CommonYesOrNoEnum.getRemark(queryResp.getType()));
			resultResp.setStatusName(TeacherStatusEnum.getRemark(queryResp.getStatus()));

			List<String> classesIds = new ArrayList<>();
			List<ClassesInfo> classesList = classesInfoDao.queryTeacherClasses(queryResp.getTeacherId());
			for (ClassesInfo info : classesList) {
				classesIds.add(info.getClassName());
			}
			resultResp.setClassesIds(classesIds);

			resultList.add(resultResp);
		}

		result.setRecords(resultList);
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchSave(List<BatchImportTeacherEntity> importEntityList, Long partyId, Long createUserId, Long schoolId) {
		log.info("TeacherServiceImpl batchSave partyId={},createUserId={}", partyId, createUserId);
		try {
			for (BatchImportTeacherEntity importEntity : importEntityList) {
				AddTeacherReq addRequest = new AddTeacherReq();
				BeanUtil.copyProperties(importEntity, addRequest);

				addRequest.setSchoolId(schoolId);
				addRequest.setPartyId(partyId);
				addRequest.setCreateUserId(createUserId);

				this.save(addRequest);
			}
		} catch (Exception e) {
			log.info("TeacherServiceImpl batchSave e={}", e);
			throw new SysException();
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int save(AddTeacherReq addRequest) {
		log.info("TeacherServiceImpl save request={}", addRequest);
		// 判断手机号码是否已注册
		int userType = 1;// 类型: 1老师 2家长 3老师兼家长
		boolean newFlag = true;
		AppUserResp queryAppUser = appUserService.queryAppUser(addRequest.getPhone());
		if (null != queryAppUser) {
			if (queryAppUser.getUserType() == 2) {
				userType = 3;
				newFlag = false;
			} else {
				throw new AppException(BasicErrorCode.ENTITY_IS_EXISTS, addRequest.getPhone());
			}
		}

		Long sysUserId = addRequest.getCreateUserId();
		Date date = new Date();
		Teacher teacher = new Teacher();
		BeanUtil.copyProperties(addRequest, teacher);
		int i = 0;
		Long appUserId = 0l;
		try {
			if (newFlag) {
				// 新增app_user 密码zhty+手机后四位
				String password = "zhty" + addRequest.getPhone().substring(addRequest.getPhone().length() - 4,
						addRequest.getPhone().length());
				AddAppUserReq addAppUser = new AddAppUserReq();
				addAppUser.setUserType(userType);
				addAppUser.setMobile(addRequest.getPhone());
				addAppUser.setPassword(BCryptUtil.encode(password));
				addAppUser.setRealName(addRequest.getName());
				addAppUser.setCreateTime(date);
				appUserId = appUserService.save(addAppUser);

				String account=getPinyin(addRequest.getName(),teacher.getTeacherId());

				Teacher updateTeacher = new Teacher();
				updateTeacher.setTeacherId(teacher.getTeacherId());
				updateTeacher.setAccount(account);
				updateTeacher.setUpdateTime(date);
				teacherDao.updateById(updateTeacher);


				TeacherResponse r=new TeacherResponse();
				r.setId(teacher.getTeacherId().toString());
				r.setName(teacher.getName());
				r.setStaffNumber("");
				r.setAccount(account);
				r.setPassword(password);
				r.setRole(teacher.getType().toString());
				r.setGender(teacher.getSex()== 0 ? "1" :"2");
				r.setCampusId(teacher.getSchoolId().toString());
				synXunfeiService.pushDate(r, XunFeiPushEventApiService.EVENT_TEACHER,XunFeiPushEventApiService.INSERT,teacher.getPartyId().toString(),sysUserId.toString());

			} else {
				appUserId = queryAppUser.getAppUserId();
				UpdateAppUserReq updateAppUser = new UpdateAppUserReq();
				updateAppUser.setAppUserId(appUserId);
				updateAppUser.setUserType(userType);
				updateAppUser.setUpdateTime(date);
				appUserService.update(updateAppUser);
			}

			// 新增老师信息
			teacher.setAppUserId(appUserId);
			teacher.setCreateTime(date);
			teacher.setCreateUserId(sysUserId);
			i = teacherDao.insert(teacher);

			// 新增老师班级信息
			if (null != addRequest.getClassesIds()) {
				for (Long classId : addRequest.getClassesIds()) {
					TeacherClass teacherClass = new TeacherClass();
					teacherClass.setTeacherId(teacher.getTeacherId());
					teacherClass.setClassId(classId);
					teacherClass.setCreateUserId(sysUserId);
					teacherClass.setCreateTime(date);
					teacherClassDao.insert(teacherClass);
				}
			}
		} catch (Exception e) {
			log.info("TeacherServiceImpl save e={}", e);
			throw new SysException();
		}
		return i;
	}

	@Override
	public String getPinyin(String name, Long teacherId) {
		String namePinyin = PinyinTool.convertHanzi2Pinyin(name, true);
		Teacher queryTeacher = teacherDao.selectOne(Wrappers.lambdaQuery(Teacher.class)
				.eq(Teacher::getAccount, namePinyin));
		if (null != queryTeacher) {
			namePinyin = namePinyin + teacherId;
		}
		return namePinyin;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int update(UpdateTeacherReq updateRequest) {
		log.info("TeacherServiceImpl update request={}", updateRequest);
		Teacher queryTeacher = teacherDao.selectById(updateRequest.getTeacherId());
		if (null == queryTeacher) {
			throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "老师");
		}

		boolean updateUserFlag = false;
		AppUserResp appUser = appUserService.queryAppUser(queryTeacher.getAppUserId());
		if (!updateRequest.getPhone().equals(appUser.getMobile())) {
			// 判断手机号码是否已注册
			AppUserResp appUserResp = appUserService.queryAppUser(updateRequest.getPhone());
			if (null != appUserResp) {
				throw new AppException(BasicErrorCode.ENTITY_IS_EXISTS, updateRequest.getPhone());
			}

			updateUserFlag = true;
		}

		Long userId = updateRequest.getUpdateUserId();
		Date date = new Date();
		Teacher updateTeacher = new Teacher();
		BeanUtil.copyProperties(updateRequest, updateTeacher);
		int i = 0;
		try {
			if (updateUserFlag) {
				UpdateAppUserReq updateAppUserReq = new UpdateAppUserReq();
				String password = "zhty" + updateRequest.getPhone().substring(updateRequest.getPhone().length() - 4,
						updateRequest.getPhone().length());
				updateAppUserReq.setAppUserId(queryTeacher.getAppUserId());
				updateAppUserReq.setMobile(updateRequest.getPhone());
				updateAppUserReq.setPassword(BCryptUtil.encode(password));
				updateAppUserReq.setUpdateTime(date);
				appUserService.update(updateAppUserReq);
			}

			updateTeacher.setUpdateUserId(userId);
			updateTeacher.setUpdateTime(date);
			i = teacherDao.updateById(updateTeacher);

			TeacherResponse r=new TeacherResponse();
			r.setId(queryTeacher.getTeacherId().toString());
			r.setName(updateTeacher.getName());
			r.setStaffNumber("");
			r.setRole(updateTeacher.getType().toString());
			r.setGender(updateTeacher.getSex()== 0 ? "1" :"2");
			r.setCampusId(queryTeacher.getSchoolId().toString());
			synXunfeiService.pushDate(r, XunFeiPushEventApiService.EVENT_TEACHER,XunFeiPushEventApiService.UPDATE,queryTeacher.getPartyId().toString(),userId.toString());



			if (updateRequest.getClassesIds().size() > 0) {
				// 删除之前所有任教班级
				List<TeacherClass> queryTeacherClassList = teacherClassDao.selectList(Wrappers
						.lambdaQuery(TeacherClass.class).eq(TeacherClass::getTeacherId, queryTeacher.getTeacherId())
						.eq(TeacherClass::getDelFlag, 0));
				for (TeacherClass temp : queryTeacherClassList) {
					TeacherClass updateTeacherClass = new TeacherClass();
					updateTeacherClass.setId(temp.getId());
					updateTeacherClass.setDelFlag(1);
					updateTeacherClass.setUpdateTime(date);
					updateTeacherClass.setUpdateUserId(userId);

					teacherClassDao.updateById(updateTeacherClass);
				}

				// 重新增加任教班级
				for (Long classId : updateRequest.getClassesIds()) {
					TeacherClass teacherClass = new TeacherClass();
					teacherClass.setTeacherId(queryTeacher.getTeacherId());
					teacherClass.setClassId(classId);
					teacherClass.setCreateUserId(userId);
					teacherClass.setCreateTime(date);

					teacherClassDao.insert(teacherClass);
				}
			}

		} catch (Exception e) {
			log.info("TeacherServiceImpl update e={}", e);
			throw new SysException();
		}
		return i;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchDelete(BatchOperateTeacherReq request) {
		log.info("TeacherServiceImpl batchDelete request={}", request);
		Date date = new Date();
		try {
			for (Long teacherId : request.getTeacherIds()) {
				Teacher queryTeacher = teacherDao.selectById(teacherId);
				if (null != queryTeacher) {
					// 注销前端登录帐号
					UpdateAppUserReq updateAppUserReq = new UpdateAppUserReq();
					updateAppUserReq.setStatus(0);
					updateAppUserReq.setAppUserId(queryTeacher.getAppUserId());
					updateAppUserReq.setUpdateTime(date);
					appUserService.update(updateAppUserReq);

					// 删除老师
					Teacher updateTeacher = new Teacher();
					updateTeacher.setTeacherId(queryTeacher.getTeacherId());
					updateTeacher.setDelFlag(1);
					updateTeacher.setStatus(0);
					updateTeacher.setUpdateTime(date);
					updateTeacher.setUpdateUserId(request.getUpdateUserId());
					teacherDao.updateById(updateTeacher);


					TeacherResponse r=new TeacherResponse();
					r.setId(queryTeacher.getTeacherId().toString());
					r.setName(queryTeacher.getName());
					r.setStaffNumber(queryTeacher.getAccount());
					r.setRole(queryTeacher.getType().toString());
					r.setGender(queryTeacher.getSex()== 0 ? "1" :"2");
					r.setCampusId(queryTeacher.getSchoolId().toString());
					synXunfeiService.pushDate(r, XunFeiPushEventApiService.EVENT_TEACHER,XunFeiPushEventApiService.DELETE,queryTeacher.getPartyId().toString(),request.getUpdateUserId().toString());

					// 删除任教班级
					List<TeacherClass> queryTeacherClassList = teacherClassDao.selectList(
							Wrappers.lambdaQuery(TeacherClass.class).eq(TeacherClass::getTeacherId, queryTeacher.getTeacherId())
									.eq(TeacherClass::getDelFlag, 0));
					for (TeacherClass temp : queryTeacherClassList) {
						TeacherClass updateTeacherClass = new TeacherClass();
						updateTeacherClass.setId(temp.getId());
						updateTeacherClass.setDelFlag(1);
						updateTeacherClass.setUpdateTime(date);
						updateTeacherClass.setUpdateUserId(request.getUpdateUserId());

						teacherClassDao.updateById(updateTeacherClass);
					}
				}
			}
		} catch (Exception e) {
			log.info("TeacherServiceImpl batchDelete e={}", e);
			throw new SysException();
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int delete(Long teacherId) {
		log.info("TeacherServiceImpl delete teacherId={}", teacherId);
		Teacher queryTeacher = teacherDao.selectById(teacherId);
		if (null == queryTeacher) {
			throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "老师");
		}

		int i = 0;
		try {
			Date date = new Date();

			// 注销前端登录帐号
			UpdateAppUserReq updateAppUserReq = new UpdateAppUserReq();
			updateAppUserReq.setStatus(0);
			updateAppUserReq.setAppUserId(queryTeacher.getAppUserId());
			updateAppUserReq.setUpdateTime(date);
			appUserService.update(updateAppUserReq);

			// 删除老师
			Teacher updateTeacher = new Teacher();
			updateTeacher.setTeacherId(queryTeacher.getTeacherId());
			updateTeacher.setDelFlag(1);
			updateTeacher.setStatus(0);
			updateTeacher.setUpdateTime(date);

			i = teacherDao.updateById(updateTeacher);


			TeacherResponse r=new TeacherResponse();
			r.setId(queryTeacher.getTeacherId().toString());
			r.setName(queryTeacher.getName());
			r.setStaffNumber(queryTeacher.getAccount());
			r.setRole(queryTeacher.getType().toString());
			r.setGender(queryTeacher.getSex()== 0 ? "1" :"2");
			r.setCampusId(queryTeacher.getSchoolId().toString());
			synXunfeiService.pushDate(r, XunFeiPushEventApiService.EVENT_TEACHER,XunFeiPushEventApiService.DELETE,queryTeacher.getPartyId().toString(),queryTeacher.getCreateUserId().toString());


			// 删除任教班级
			List<TeacherClass> queryTeacherClassList = teacherClassDao.selectList(
					Wrappers.lambdaQuery(TeacherClass.class).eq(TeacherClass::getTeacherId, queryTeacher.getTeacherId())
							.eq(TeacherClass::getDelFlag, 0));
			for (TeacherClass temp : queryTeacherClassList) {
				TeacherClass updateTeacherClass = new TeacherClass();
				updateTeacherClass.setId(temp.getId());
				updateTeacherClass.setDelFlag(1);
				updateTeacherClass.setUpdateTime(date);

				teacherClassDao.updateById(updateTeacherClass);
			}
		} catch (Exception e) {
			log.info("TeacherServiceImpl delete e={}", e);
			throw new SysException();
		}
		return i;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchEnable(BatchOperateTeacherReq request) {
		log.info("TeacherServiceImpl batchEnable request={}", request);
		Date date = new Date();
		try {
			for (Long teacherId : request.getTeacherIds()) {
				Teacher queryTeacher = teacherDao.selectById(teacherId);
				if (null != queryTeacher) {
					UpdateAppUserReq updateAppUserReq = new UpdateAppUserReq();
					updateAppUserReq.setStatus(2);
					updateAppUserReq.setAppUserId(queryTeacher.getAppUserId());
					updateAppUserReq.setUpdateTime(date);
					appUserService.update(updateAppUserReq);

					Teacher updateTeacher = new Teacher();
					updateTeacher.setTeacherId(queryTeacher.getTeacherId());
					updateTeacher.setStatus(1);
					updateTeacher.setUpdateTime(date);
					updateTeacher.setUpdateUserId(request.getUpdateUserId());

					teacherDao.updateById(updateTeacher);
				}
			}
		} catch (Exception e) {
			log.info("TeacherServiceImpl batchEnable e={}", e);
			throw new SysException();
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void enable(Long teacherId) {
		log.info("TeacherServiceImpl enable teacherId={}", teacherId);
		Teacher queryTeacher = teacherDao.selectById(teacherId);
		if (null == queryTeacher) {
			throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "老师");
		}

		try {
			Date date = new Date();

			UpdateAppUserReq updateAppUserReq = new UpdateAppUserReq();
			updateAppUserReq.setStatus(2);
			updateAppUserReq.setAppUserId(queryTeacher.getAppUserId());
			updateAppUserReq.setUpdateTime(date);
			appUserService.update(updateAppUserReq);

			Teacher updateTeacher = new Teacher();
			updateTeacher.setTeacherId(queryTeacher.getTeacherId());
			updateTeacher.setStatus(1);
			updateTeacher.setUpdateTime(date);

			teacherDao.updateById(updateTeacher);
		} catch (Exception e) {
			log.info("TeacherServiceImpl enable e={}", e);
			throw new SysException();
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchDisabled(BatchOperateTeacherReq request) {
		log.info("TeacherServiceImpl batchDisabled request={}", request);
		Date date = new Date();
		try {
			for (Long teacherId : request.getTeacherIds()) {
				Teacher queryTeacher = teacherDao.selectById(teacherId);
				if (null != queryTeacher) {
					UpdateAppUserReq updateAppUserReq = new UpdateAppUserReq();
					updateAppUserReq.setStatus(1);
					updateAppUserReq.setAppUserId(queryTeacher.getAppUserId());
					updateAppUserReq.setUpdateTime(date);
					appUserService.update(updateAppUserReq);

					Teacher updateTeacher = new Teacher();
					updateTeacher.setTeacherId(queryTeacher.getTeacherId());
					updateTeacher.setStatus(0);
					updateTeacher.setUpdateTime(date);
					updateTeacher.setUpdateUserId(request.getUpdateUserId());

					teacherDao.updateById(updateTeacher);
				}
			}
		} catch (Exception e) {
			log.info("TeacherServiceImpl batchDisabled e={}", e);
			throw new SysException();
		}

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void disabled(Long teacherId) {
		log.info("TeacherServiceImpl disabled teacherId={}", teacherId);
		Teacher queryTeacher = teacherDao.selectById(teacherId);
		if (null == queryTeacher) {
			throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "老师");
		}

		try {
			Date date = new Date();

			UpdateAppUserReq updateAppUserReq = new UpdateAppUserReq();
			updateAppUserReq.setStatus(1);
			updateAppUserReq.setAppUserId(queryTeacher.getAppUserId());
			updateAppUserReq.setUpdateTime(date);
			appUserService.update(updateAppUserReq);

			Teacher updateTeacher = new Teacher();
			updateTeacher.setTeacherId(queryTeacher.getTeacherId());
			updateTeacher.setStatus(0);
			updateTeacher.setUpdateTime(date);

			teacherDao.updateById(updateTeacher);

		} catch (Exception e) {
			log.info("TeacherServiceImpl disabled e={}", e);
			throw new SysException();
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void reset(Long teacherId) {
		log.info("TeacherServiceImpl reset teacherId={}", teacherId);
		Teacher queryTeacher = teacherDao.selectById(teacherId);
		if (null == queryTeacher) {
			throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "老师");
		}

		try {
			AppUserResp user = appUserService.queryAppUser(queryTeacher.getAppUserId());
			if (null != user) {
				String mobile = user.getMobile();
				UpdateAppUserReq updateAppUserReq = new UpdateAppUserReq();
				String password = "zhty" + mobile.substring(mobile.length() - 4, mobile.length());
				updateAppUserReq.setAppUserId(queryTeacher.getAppUserId());
				updateAppUserReq.setPassword(BCryptUtil.encode(password));
				updateAppUserReq.setUpdateTime(new Date());
				appUserService.update(updateAppUserReq);
			}
		} catch (Exception e) {
			log.info("TeacherServiceImpl reset e={}", e);
			throw new SysException();
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchReset(BatchOperateTeacherReq request) {
		log.info("TeacherServiceImpl batchReset request={}", request);
		Date date = new Date();
		try {
			for (Long teacherId : request.getTeacherIds()) {
				Teacher queryTeacher = teacherDao.selectById(teacherId);
				if (null != queryTeacher) {
					AppUserResp user = appUserService.queryAppUser(queryTeacher.getAppUserId());
					String mobile = user.getMobile();
					UpdateAppUserReq updateAppUserReq = new UpdateAppUserReq();
					String password = "zhty" + mobile.substring(mobile.length() - 4, mobile.length());
					updateAppUserReq.setAppUserId(queryTeacher.getAppUserId());
					updateAppUserReq.setPassword(BCryptUtil.encode(password));
					updateAppUserReq.setUpdateTime(date);
					appUserService.update(updateAppUserReq);
				}
			}
		} catch (Exception e) {
			log.info("TeacherServiceImpl batchReset e={}", e);
			throw new SysException();
		}
	}

}
